
from . import models


def save_to_db_大中型水库名录(api_result):
    for row in api_result:
        # 过滤掉row中的非表json字段
        keys = [k for k in row]
        for key in keys:
            if key not in models.fieldsReservoirNameList:
                del row[key]

        insert_obj = models.ReservoirNameList.insert(**dict(row))
        # print(insert_obj)
        replace_obj = insert_obj.on_conflict_replace()
        # print(replace_obj)
        replace_obj.execute()


def save_to_db_大中型水库基础信息(api_result):
    for row in api_result:
        # 过滤掉row中的非表json字段
        keys = [k for k in row]
        for key in keys:
            if key not in models.fieldsReservoirBaseInfo:
                del row[key]

        insert_obj = models.ReservoirBaseInfo.insert(**dict(row))
        # print(insert_obj)
        replace_obj = insert_obj.on_conflict_replace()
        # print(replace_obj)
        replace_obj.execute()


def save_to_db_河道水位监测数据(api_result):
    for row in api_result:
        # 过滤掉row中的非表json字段
        keys = [k for k in row]
        for key in keys:
            if key not in models.fieldsRiverWaterLevel:
                del row[key]

        insert_obj = models.RiverWaterLevel.insert(**dict(row))
        # print(insert_obj)
        replace_obj = insert_obj.on_conflict_replace()
        # print(replace_obj)
        replace_obj.execute()
